/**

 @Name：layuiAdmin 用户管理 管理员管理 角色管理
 @Author：star1029
 @Site：http://www.layui.com/admin/
 @License：LPPL
    
 */


layui.define(['table', 'form', "tree"], function(exports) {
	var $ = layui.$,
		setter = layui.setter,
		admin = layui.admin,
		view = layui.view,
		table = layui.table,
		tree = layui.tree,
		form = layui.form;

	//管理员管理
	table.render({
		elem: '#LAY-role-back-manage',
		method:"post",
		url: setter.api_source + 'sysrole/list',
		parseData: function(res) { //res 即为原始返回的数据
			return {
				"code": res.code, //解析接口状态
				"msg": res.message, //解析提示文本
				"count": res.data.total, //解析数据长度
				"data": res.data.rows //解析数据列表
			};
		},
		cols: [
			[{
				type: 'checkbox',
				fixed: 'left'
			}, {
				field: 'id',
				width: 80,
				title: 'ID',
				sort: true
			}, {
				field: 'name',
				title: '昵称'
			},{
				field: 'state',
				title: '状态',
				templet: '#buttonTpl',
				minWidth: 80,
				align: 'center'
			}, {
				title: '操作',
				width: 250,
				align: 'center',
				fixed: 'right',
				toolbar: '#table-sysrole-admin'
			}]
		],
		text: '对不起，加载出现异常！',
		page: true,
		limit: 10,
		limits: [5, 10, 15],

	});

	

	//监听工具条
	table.on('tool(LAY-role-back-manage)', function(obj) {
		var data = obj.data;
		var tempdata = obj.data;
		if (obj.event === 'del') {
			layer.prompt({
				formType: 1,
				title: '敏感操作，请验证口令'
			}, function(value, index) {
				layer.close(index);
				layer.confirm('确定删除此管理员？', function(index) {
					
					// console.log(obj);
					
					admin.req({
						type: "get",
						url: setter.api_source + 'sysrole/delete/'+obj.data.id,
						beforeSend: function(XMLHttpRequest) {
							//ShowLoading();
						},
						success: function(data, textStatus) {
							obj.del();
							layer.close(index);
						},
						complete: function(XMLHttpRequest, textStatus) {
							//HideLoading();
						},
						error: function() {
							//请求出错处理
						}
					});
				});
			});
		} else if (obj.event === 'edit') {
			admin.popup({
				title: '编辑管理员',
				area: ['420px', '450px'],
				id: 'LAY-popup-user-add',
				success: function(layero, index) {
					
					// 指定要渲染的模板html
					view(this.id).render('user/administrators/role/sysroleeditform', tempdata).done(function() {
						// 指定渲染的fiter
						form.render(null, 'sysrole-edit-form');
						//监听提交
						form.on('submit(LAY-role-back-submit)', function(data) {
							var field = data.field; //获取提交的字段

							
							// 解决switch控件关闭时没有值的情况
							var status = "1";
							if (field.status != null) {
								status = field.status;
							}
							field["status"] = status;
					
							// console.log(field);
							admin.req({
								type: "post",
								data: {
									"id":field.id,
									"name":field.name,
									"status":field.status
								},
								url: setter.api_source + 'sysrole/update',
								beforeSend: function(XMLHttpRequest) {
									//ShowLoading();
								},
								success: function(data, textStatus) {
									layui.table.reload('LAY-role-back-manage'); //重载表格
									layer.close(index); //执行关闭 
								},
								complete: function(XMLHttpRequest, textStatus) {
									//HideLoading();
								},
								error: function() {
									//请求出错处理
								}
							});
						});
					});
				}
			});
		} else if (obj.event === 'func') {
			admin.popup({
				title: '权限配置',
				area: ['420px', '450px'],
				id: 'LAY-popup-userfunc-edit',
				success: function(layero, index) {
					
					// 指定要渲染的模板html
					view(this.id).render('user/administrators/role/sysfunceditform').done(function() {
						// console.log(this.id);
						admin.req({
							type: "get",
							// contentType: 'application/json; charset=UTF-8',
							url: setter.api_source + 'rolemenu/getRolePermissionByRoleId/'+data.id,
							beforeSend: function(XMLHttpRequest) {
								//ShowLoading();
							},
							success: function(data, textStatus) {
								// console.log(data.data.tree);
								
								tree.render({
								  elem: '#role-menu-tree'
								  ,data: data.data.tree
								  ,showCheckbox: true  //是否显示复选框
								  ,id: 'role-menu-tree-1'
								});
							},
							complete: function(XMLHttpRequest, textStatus) {
								// layui.table.reload('LAY-role-back-manage'); //重载表格
								// layer.close(index); //执行关闭 
							},
							error: function() {
								//请求出错处理
								layer.msg('删除错误');
							}
						});
						
						
						//监听提交
						form.on('submit(LAY-role-back-submit)', function(data) {
							var field = data.field; //获取提交的字段
							// console.log(field);
							admin.req({
								type: "post",
								contentType: 'application/json; charset=UTF-8',
								url: setter.api_source + 'rolemenu/setRolePermission/'+tempdata.id,
								data:JSON.stringify(field),
								beforeSend: function(XMLHttpRequest) {
									//ShowLoading();
								},
								success: function(data, textStatus) {
									layer.msg(data.message);
								},
								complete: function(XMLHttpRequest, textStatus) {
									layui.table.reload('LAY-role-back-manage'); //重载表格
									layer.close(index); //执行关闭 
								},
								error: function() {
									//请求出错处理
									
								}
							});
						});
					});
				}
			});
		}
	});

	exports('sysrole', {})
});
